﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_User : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Kiem tra co dang nhap khong?
        if (Session["MaNguoiDung"] == null || Session["MaNguoiDung"].ToString() == string.Empty)
        {
            lblMaNguoiDung.Text = ".....";

            Response.Redirect("~/pHome.aspx");
        }
        else
        {
            lblMaNguoiDung.Text = Session["MaNguoiDung"].ToString();
        }
    }
    protected void lbtnDangXuat_Click(object sender, EventArgs e)
    {
        Session.Clear();

        Response.Redirect("~/pHome.aspx");
    }
    protected void chkSoVanBan_CheckedChanged(object sender, EventArgs e)
    {
        if (chkSoVanBan.Checked)
        {
            txtSoVanBan.Enabled = true;

            txtSoVanBan.Text = string.Empty;

            if (chkTenVanBan.Checked)
            {
                chkTenVanBan.Checked = false;
                
                txtTenVanBan.Enabled = false;
                
                txtTenVanBan.Text = string.Empty;
            }
        }
        else
        {
            txtSoVanBan.Enabled = false;

            txtSoVanBan.Text = string.Empty;
        }
    }
    protected void chkTenVanBan_CheckedChanged(object sender, EventArgs e)
    {
        if (chkTenVanBan.Checked)
        {
            txtTenVanBan.Enabled = true;

            txtTenVanBan.Text = string.Empty;

            if (chkSoVanBan.Checked)
            {
                chkSoVanBan.Checked = false;

                txtSoVanBan.Enabled = false;

                txtSoVanBan.Text = string.Empty;
            }
        }
        else
        {
            txtTenVanBan.Enabled = false;

            txtTenVanBan.Text = string.Empty;
        }
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        MyTextBox myTextBox = new MyTextBox();

        if (myTextBox.kiemtraRong(txtNgayTu) == false && myTextBox.kiemtraRong(txtNgayDen) == false)
        {
            if (DateTime.ParseExact(txtNgayDen.Text, "dd/MM/yyyy", null) >= DateTime.ParseExact(txtNgayTu.Text, "dd/MM/yyyy", null))
            {
                string soVanBan = txtSoVanBan.Text.ToUpper();

                string tenVanBan = txtTenVanBan.Text;

                string ngayTu = txtNgayTu.Text;
                if (ngayTu != string.Empty)
                {
                    ngayTu = txtNgayTu.Text;
                }

                string ngayDen = txtNgayDen.Text;
                if (ngayDen != string.Empty)
                {
                    ngayDen = txtNgayDen.Text;
                }

                //Dung bien Session de giu cac gia tri lai
                Session["SoVanBan"] = soVanBan;
                Session["TenVanBan"] = tenVanBan;
                Session["NgayTu"] = ngayTu;
                Session["NgayDen"] = ngayDen;

                Response.Redirect("KetQuaTimKiem.aspx");
            }
            else
            {
                Alert.Show("Ngay den khong duoc nho hon Ngay tu");

                return;
            }
        }

        if (myTextBox.kiemtraRong(txtNgayTu) == true && myTextBox.kiemtraRong(txtNgayDen) == true)
        {
            string soVanBan = txtSoVanBan.Text.ToUpper();

            string tenVanBan = txtTenVanBan.Text;

            string ngayTu = txtNgayTu.Text;
            if (ngayTu != string.Empty)
            {
                ngayTu = txtNgayTu.Text;
            }

            string ngayDen = txtNgayDen.Text;
            if (ngayDen != string.Empty)
            {
                ngayDen = txtNgayDen.Text;
            }

            //Dung bien Session de giu cac gia tri lai
            Session["SoVanBan"] = soVanBan;
            Session["TenVanBan"] = tenVanBan;
            Session["NgayTu"] = ngayTu;
            Session["NgayDen"] = ngayDen;

            Response.Redirect("~/QLVB/User/KetQuaTimKiem.aspx");
        }

        if ((myTextBox.kiemtraRong(txtNgayTu) == false && myTextBox.kiemtraRong(txtNgayDen) == true) || (myTextBox.kiemtraRong(txtNgayTu) == true && myTextBox.kiemtraRong(txtNgayDen) == false))
        {
            Alert.Show("Xin vui long nhap ngay can tim kiem con lai");

            return;
        }
    }
    protected void lbtnTimKiemVanBanTheoMaNguoiDung_Click(object sender, EventArgs e)
    {
        MyTextBox myTextBox = new MyTextBox();

        string soVanBan = txtSoVanBan.Text.ToUpper();

        string tenVanBan = txtTenVanBan.Text;

        string ngayTu = txtNgayTu.Text;
        if (ngayTu != string.Empty)
        {
            ngayTu = txtNgayTu.Text;
        }

        string ngayDen = txtNgayDen.Text;
        if (ngayDen != string.Empty)
        {
            ngayDen = txtNgayDen.Text;
        }

        //Dung bien Session de giu cac gia tri lai
        Session["SoVanBan"] = soVanBan;
        Session["TenVanBan"] = tenVanBan;
        Session["NgayTu"] = ngayTu;
        Session["NgayDen"] = ngayDen;

        Response.Redirect("KetQuaTimKiemTheoNguoiDung.aspx");
    }
    protected void lbtnVanBanDaGuiTheoNgay_Click(object sender, EventArgs e)
    {
        string maNguoiDung = (String)Session["MaNguoiDung"];
        if (maNguoiDung != null)
        {
            //Phan quyen chi co nguoi dung phong TCHC duoc vao trang nay
            PhongBUS pBUS = new PhongBUS();
            string maPhong = pBUS.layMaPhongTheoMaNguoiDung((String)Session["MaNguoiDung"]);
            switch (maPhong)
            {
                case "TCHC":
                    Response.Redirect("VanBanDaGui.aspx");
                    break;

                default:
                    Alert.Show("Ban khong co quyen su dung chuc nang nay");
                    
                    break;
            }
        }
        else
        {
            Alert.Show("Xin vui long dang nhap truoc khi thuc hien chuc nang nay");

            return;
        }
    }
    protected void lbtnDoiMatKhau_Click(object sender, EventArgs e)
    {
        string maNguoiDung = (String)Session["MaNguoiDung"];

        if (maNguoiDung != null)
        {
            Response.Redirect("DoiMatKhau.aspx");
        }
        else
        {
            Alert.Show("Xin vui long dang nhap truoc khi thuc hien chuc nang nay");

            return;
        }
    }
    protected void lbtnSuaVanBanChuaDuyet_Click(object sender, EventArgs e)
    {
        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
        string maLoaiNguoiDung = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung((String)Session["MaNguoiDung"]);

        switch (maLoaiNguoiDung)
        {
            case "USER":
                Response.Redirect("VanBanChuaDuyet.aspx");
                break;

            default:
                Alert.Show("Ban khong co chuc soan thao van ban. Ban khong the su dung chuc nang nay");
                return;
        }
    }
    protected void lbtnVanBanDaDuyet_Click(object sender, EventArgs e)
    {
        Response.Redirect("VanBanDaDuyet.aspx");
    }
    protected void lbtnPhieuTrinh_Click(object sender, EventArgs e)
    {
        string maNguoiDung = (String)Session["MaNguoiDung"];
        if (maNguoiDung != null)
        {
            //Phan quyen chi co nguoi dung phong TCHC duoc vao trang nay
            PhongBUS pBUS = new PhongBUS();
            string maPhong = pBUS.layMaPhongTheoMaNguoiDung((String)Session["MaNguoiDung"]);
            switch (maPhong)
            {
                case "TCHC":
                    Response.Redirect("PhieuTrinh.aspx");
                    break;

                default:
                    Alert.Show("Ban khong co quyen su dung chuc nang nay");
                    
                    break;
            }
        }
        else
        {
            Alert.Show("Xin vui long dang nhap truoc khi thuc hien chuc nang nay");

            return;
        }
    }
    protected void lbtnDuyetMauBieu_Click(object sender, EventArgs e)
    {
        //PhanQuyen
        string maNguoiDung = (String)Session["MaNguoiDung"];
        Session["MaPhong"] = (new PhongBUS()).layMaPhongTheoMaNguoiDung(maNguoiDung);

        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
        Session["MaLoaiNguoiDung"] = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDung);
        switch (Session["MaLoaiNguoiDung"].ToString())
        {
            case "KSCN":
                Response.Redirect("DuyetSoanThaoMauBieu.aspx");
                break;

            case "KSPT":
                Response.Redirect("DuyetSoanThaoMauBieu.aspx");
                break;

            default:
                Alert.Show("Ban khong co quyen thuc hien chuc nang nay");

                return;
        }
    }
    protected void lbtnSoanThaoMauBieu_Click(object sender, EventArgs e)
    {
        //PhanQuyen
        string maNguoiDung = (String)Session["MaNguoiDung"];
        Session["MaPhong"] = (new PhongBUS()).layMaPhongTheoMaNguoiDung(maNguoiDung);

        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
        Session["MaLoaiNguoiDung"] = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDung);
        switch (Session["MaLoaiNguoiDung"].ToString())
        {
            case "USER":
                Response.Redirect("SoanThaoMauBieu.aspx");
                break;

            default:
                Alert.Show("Ban khong co quyen thuc hien chuc nang nay");

                return;
        }
    }
    protected void lbtnMauBieuChuaDuyet_Click(object sender, EventArgs e)
    {
        //PhanQuyen
        string maNguoiDung = (String)Session["MaNguoiDung"];
        Session["MaPhong"] = (new PhongBUS()).layMaPhongTheoMaNguoiDung(maNguoiDung);

        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
        Session["MaLoaiNguoiDung"] = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDung);
        switch (Session["MaLoaiNguoiDung"].ToString())
        {
            case "USER":
                Response.Redirect("MauBieuChuaDuyet.aspx");
                break;

            default:
                Alert.Show("Ban khong co quyen thuc hien chuc nang nay");

                return;
        }
    }
}
